package tt.dz.biz.service.resource;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.resource.ResourceDAOImpl;
import tt.dz.common.LogUtil;
import tt.dz.vo.ResourceVO;
import tt.dz.vo.RoleResourceVO;
import tt.dz.vo.RoleVO;

@Service("resourceService")
public class ResourceService
{
  @Resource(name="resourceDAO")
  private ResourceDAOImpl resourceDao;
  
  public List<ResourceVO> queryResourceByRole(RoleVO roleVO)
  {
    try
    {
      return this.resourceDao.queryResourceByRole(roleVO);
    }
    catch (Exception e)
    {
      LogUtil.error("查询角色资源异常", e);
    }
    return new ArrayList();
  }
  
  public List<ResourceVO> queryUnAllotResourceByRole(RoleVO roleVO)
  {
    try
    {
      return this.resourceDao.queryUnAllotResourceByRole(roleVO);
    }
    catch (Exception e)
    {
      LogUtil.error("查询角色资源异常", e);
    }
    return new ArrayList();
  }
  
  public boolean batchModifyRoleResource(List<RoleResourceVO> roleResourceVOs)
  {
    try
    {
      List<RoleResourceVO> rrvs = findNeedRoleResourceVOs(roleResourceVOs);
      for (RoleResourceVO roleResourceVO : rrvs) {
        if (roleResourceVO.getIsValid().intValue() == 0) {
          this.resourceDao.deleteRoleResource(roleResourceVO);
        } else if (this.resourceDao.getRoleResourceCount(roleResourceVO) > 0) {
          this.resourceDao.updateRoleResource(roleResourceVO);
        } else {
          this.resourceDao.insertRoleResource(roleResourceVO);
        }
      }
      return true;
    }
    catch (Exception e)
    {
      LogUtil.error("修改角色资源关系异常", e);
    }
    return false;
  }
  
  public List<RoleResourceVO> findNeedRoleResourceVOs(List<RoleResourceVO> roleResourceVOs)
  {
    RoleResourceVO roleResourceVO = null;
    
    String parentResourceId = null;
    String[] parentResourceIds = null;
    
    List<String> resourceIds = initResourceIds(roleResourceVOs);
    
    List<RoleResourceVO> ret = new ArrayList();
    ret.addAll(roleResourceVOs);
    for (RoleResourceVO tempRoleResourceVO : roleResourceVOs) {
      if (tempRoleResourceVO.getIsValid().intValue() == 1)
      {
        parentResourceId = tempRoleResourceVO.getParentResourceId();
        if (parentResourceId != null)
        {
          parentResourceIds = parentResourceId.split(",");
          for (String temp : parentResourceIds)
          {
            roleResourceVO = new RoleResourceVO();
            roleResourceVO.setResourceId(temp);
            roleResourceVO.setRoleId(tempRoleResourceVO.getRoleId());
            roleResourceVO.setIsValid(Integer.valueOf(1));
            if (!isRepeats(resourceIds, temp)) {
              if (this.resourceDao.getResourceCount(roleResourceVO) > 0)
              {
                ret.add(roleResourceVO);
                resourceIds.add(temp);
              }
            }
          }
        }
      }
    }
    return ret;
  }
  
  public boolean isRepeats(List<String> resourceIds, String resouceId)
  {
    for (String temp : resourceIds) {
      if (temp.equals(resouceId)) {
        return true;
      }
    }
    return false;
  }
  
  public List<String> initResourceIds(List<RoleResourceVO> roleResourceVOs)
  {
    List<String> resourceIds = new ArrayList();
    for (RoleResourceVO temp : roleResourceVOs) {
      resourceIds.add(temp.getResourceId());
    }
    return resourceIds;
  }
}
